package com.app.widget

import android.content.Context
import android.graphics.Outline
import android.util.AttributeSet
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.FrameLayout
import androidx.cardview.widget.CardView
import kotlin.math.roundToInt

/**
 * Created on 2020/5/14
 */
open class CardDimensionRatioFrameLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr){

    protected open val dimensionRatio = 1f / 1f
    protected open val cornerSize = 10f
    init {
        clipToOutline = true
        val corner = cornerSize.dp
        outlineProvider = object : ViewOutlineProvider(){
            override fun getOutline(view: View, p1: Outline) {
                p1.setRoundRect(0,0,view.width,view.height,corner)
            }
        }
    }
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val parentWidthMode = MeasureSpec.getMode(widthMeasureSpec)
        val parentHeightMode = MeasureSpec.getMode(heightMeasureSpec)
        when {
            parentWidthMode == MeasureSpec.EXACTLY -> {
                val parentWidth = MeasureSpec.getSize(widthMeasureSpec)
                val parentHeight = (parentWidth / dimensionRatio).roundToInt()
                val childWidth = parentWidth - paddingLeft - paddingRight
                val childHeight = parentHeight - paddingTop - paddingBottom
                setMeasuredDimension(parentWidth, parentHeight)
                val childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY)
                val childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)
                measureChildren(childWidthMeasureSpec, childHeightMeasureSpec)
            }
            parentHeightMode == MeasureSpec.EXACTLY -> {
                val parentHeight = MeasureSpec.getSize(heightMeasureSpec)
                val parentWidth = (parentHeight * dimensionRatio).roundToInt()
                val childWidth = parentWidth - paddingLeft - paddingRight
                val childHeight = parentHeight - paddingTop - paddingBottom
                setMeasuredDimension(parentWidth, parentHeight)
                val childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY)
                val childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)
                measureChildren(childWidthMeasureSpec, childHeightMeasureSpec)
            }
            else -> {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            }
        }

    }
}

open class DimensionRatioFrameLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr){

    protected open val dimensionRatio = 1f / 1f
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val parentWidthMode = MeasureSpec.getMode(widthMeasureSpec)
        val parentHeightMode = MeasureSpec.getMode(heightMeasureSpec)
        when {
            parentWidthMode == MeasureSpec.EXACTLY -> {
                val parentWidth = MeasureSpec.getSize(widthMeasureSpec)
                val parentHeight = (parentWidth / dimensionRatio).roundToInt()
                val childWidth = parentWidth - paddingLeft - paddingRight
                val childHeight = parentHeight - paddingTop - paddingBottom
                setMeasuredDimension(parentWidth, parentHeight)
                val childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY)
                val childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)
                measureChildren(childWidthMeasureSpec, childHeightMeasureSpec)
            }
            parentHeightMode == MeasureSpec.EXACTLY -> {
                val parentHeight = MeasureSpec.getSize(heightMeasureSpec)
                val parentWidth = (parentHeight * dimensionRatio).roundToInt()
                val childWidth = parentWidth - paddingLeft - paddingRight
                val childHeight = parentHeight - paddingTop - paddingBottom
                setMeasuredDimension(parentWidth, parentHeight)
                val childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY)
                val childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)
                measureChildren(childWidthMeasureSpec, childHeightMeasureSpec)
            }
            else -> {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            }
        }

    }
}